package cn.sumpu.app.guoguojie.web.mvc.family;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.sumpu.app.guoguojie.biz.AlbumBiz;
import cn.sumpu.app.guoguojie.constant.PictureType;
import cn.sumpu.app.guoguojie.util.PictureUrl;


@Controller
public class UploadImgController {

	@Autowired
	private AlbumBiz albumBiz;

	@RequestMapping(value = "/xhUploadImage.htm")
	public String xhUploadImage(HttpServletRequest request,HttpServletResponse response) {
		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter pw = null;
		try {
			PictureUrl imageUrl = albumBiz.uploadImage(request,false,PictureType.BLOG.getLabel(),false);
			String outputJson = "{'err':'','msg':'" + imageUrl.getOriginal() + "'}";
			pw = response.getWriter();
			pw.print(JSONObject.fromObject(outputJson));
		} catch (Exception e) {
			e.printStackTrace();
			try {
				pw = response.getWriter();
				String outputJson = "{'err':'上传图片出错！'}";
				pw.print(JSONObject.fromObject(outputJson));
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}
	
	@RequestMapping(value = "/doingImgUpload.htm")
	public String doingImgUpload(HttpServletRequest request,HttpServletResponse response) {
		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter pw = null;
		try {
			PictureUrl imageUrl = albumBiz.uploadImage(request,false,PictureType.DOING.getLabel(),true);
			pw = response.getWriter();
			pw.print(JSONObject.fromObject(imageUrl));
		} catch (Exception e) {
			e.printStackTrace();
			try {
				pw = response.getWriter();
				String outputJson = "{'err':'上传图片出错！'}";
				pw.print(JSONObject.fromObject(outputJson));
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}
	
	@RequestMapping(value = "/activityImgUpload.htm")
	public String activityImgUpload(HttpServletRequest request,HttpServletResponse response) {
		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter pw = null;
		try {
			PictureUrl imageUrl = albumBiz.uploadImage(request,false,PictureType.ACTIVITY.getLabel(),false);
			pw = response.getWriter();
			pw.print(JSONObject.fromObject(imageUrl));
		} catch (Exception e) {
			e.printStackTrace();
			try {
				pw = response.getWriter();
				String outputJson = "{'err':'上传图片出错！'}";
				pw.print(JSONObject.fromObject(outputJson));
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}

}
